<!-- file project -->
<script> 
	
	$(function(){
        
		/* REMOVE DUPLICATE ID */
		var ids = [];
		$('*').each(function() {
		    if (this.id && this.id !== '') {
		        if (ids[this.id]) {
		            $(this).remove();
		        } else {
		            ids[this.id] = this
		        }
		    }
		});
		
		/* external function */
		
		$.fn.serializeObject = function()
		{
		   var o = {};
		   var a = this.serializeArray();
		   $.each(a, function() {
			   if (o[this.name]) {
				   if (!o[this.name].push) {
					   o[this.name] = [o[this.name]];
				   }
				   o[this.name].push(this.value || '');
			   } else {
				   o[this.name] = this.value || '';
			   }
		   });
		   return o;
		};
		
		/* declarisasi */
		
		task_project = $("#task_project");
		grid_project = $("#grid-project");
		pgrid_project = $("#pgrid-project");
		btnadd_project = $("#btn-add-project");
		btnedit_project = $("#btn-edit-project");
		btndelete_project = $("#btn-delete-project");
		btncancel_project =  $("#btn-cancel-project");
		btnsave_project = $("#btn-save-project");
		form_project = $("#form-project");
		
		field_project_id = $("#project_id");
		field_project_name = $("#project_name");
		field_project_code = $("#project_code");
		field_project_value = $("#project_value");
		field_project_date = $("#project_date");
        m_cabang_id = $("#m_cabang_id");
        
        loading = $(".loading");
		
		/* method started */
		
		$( "input[type=submit],input[type=button],input[type=reset], button" ).button();
			
		/* make component */
		field_project_date.datepicker({dateFormat : 'dd-mm-yy'});
        
		grid_project.jqGrid({ 
			ajaxGridOptions : {type:"POST"},
			serializeGridData : function(postdata) {return postdata; },
			postData : {task : 'LIST'},
            url: 'c_project',
			datatype: "json", 
			colNames:[
				'Nama Proyek',
                'm_cabang_id',
				'Kode Proyek',
				'Tanggal Proyek',
				'Nilai Proyek',
				'project_id'],
			colModel:[
				{ name: 'project_name'  ,index: 'project_name' , search : true},
				{ name: 'm_cabang_id'  ,index: 'm_cabang_id' , search : true,  hidden: true },
				{ name: 'project_code'  ,index: 'project_code' , search : true},
				{ name: 'project_date'  ,index: 'project_date' , search : true},
				{ name: 'project_value'  ,index: 'project_value' , search : true , formatter : 'currency' , align : 'right' , formatoptions : { decimalPlaces : 0 }},
				{ name: 'project_id'  ,index: 'project_id',key: true, hidden: true , search : false}
			],
			width: '700',
			height: 'auto',
			rowNum:10, 
			rowList:[10,20,30], 
			pager: '#pgrid-project', 
			sortname: 'project_id', 
			viewrecords: true, 
			sortorder: "desc",
			caption:"" 
		}); 
		grid_project.jqGrid('navGrid','#pgrid-project',{edit:false,add:false,del:false,search:false});
		grid_project.jqGrid('filterToolbar',{});
		
		form_project.dialog({
			autoOpen: false,
			height: "auto",
			width: "auto",
			modal: true
		});
		
		/* event */

		btnedit_project.click(function(){
			id = grid_project.jqGrid("getGridParam","selrow");
			if (id != null){
				task_project.val("UPDATE");
				dataRow = grid_project.getRowData(id);
				field_project_id.val(dataRow.project_id);
				field_project_name.val(dataRow.project_name);
				m_cabang_id.val(dataRow.m_cabang_id);
				field_project_code.val(dataRow.project_code);
				field_project_date.val(dataRow.project_date);
				field_project_value.val(dataRow.project_value);
				$(".currency").keyup();
				form_project.dialog("open");
			}
		});
		
		btndelete_project.click(function(){
			id = grid_project.jqGrid("getGridParam","selrow");
			if (id != null){
				confr = confirm("Apa Anda yakin menghapus data ini?");
				if (confr){
					$.ajax({
						type: 'POST',
						url: 'c_project',
						data: {project_id: id, task : 'DELETE'},
						success: function(response){
							if(response==1){
								grid_project.trigger('reloadGrid');
								$("#grid-project").trigger('reloadGrid');
							}
						}
					});
				}
			}
		});
		
		btnadd_project.click(function(){
			btncancel_project.click();
			task_project.val("INSERT");
			form_project.dialog("open");
		});
		
		btncancel_project.click(function(){
			form_project.dialog("close");
		});
		
		btnsave_project.click(function(){
            loading.show();
            btnsave_project.attr("disabled","disabled");
			data = form_project.serializeObject();
            console.log(data.project_code.substr(1,1));
            if (data.project_code.substr(1,1) == data.m_cabang_id){
                $.ajax({
                    url : "c_project",
                    data : data,
                    type : 'POST'
                }).done(function(response){
                    loading.hide();
                    btnsave_project.removeAttr("disabled");
                    grid_project.trigger('reloadGrid');
                    btncancel_project.click();
                });  
            }else{
                loading.hide();
                btnsave_project.removeAttr("disabled");
                alert("Kode Proyek Salah!\n Seharusnya x"+data.m_cabang_id+"xxxx");
            }

		});
		
		/* general event */
		$(".currency").keyup(function(){
			$(this).formatCurrency();
		});
		
	});
</script>

	<table id="grid-project" class="table table-striped table-bordered" ></table>
	<div id="pgrid-project"></div>
	
	<button id="btn-add-project">Tambah</button>
	<button id="btn-edit-project">Ubah</button>
	<button id="btn-delete-project">Hapus</button>
	
	<form id="form-project" title="project" disabled=true>
		<input type="hidden" value="INSERT" name="task" id="task_project">
	   <table>
			<tr style="display: none">
				<td align="right">project_id:</td>
				<td align="left"><input id="project_id" name="project_id" /></td>
			</tr>
			
			<tr > <td align="right">Nama Proyek:</td> <td align="left"><input id="project_name" name="project_name" /></td> </tr>
			<tr > <td align="right">Kode Proyek:</td> <td align="left"><input id="project_code" name="project_code" /></td> </tr>
           <?php if(@$_SESSION[SESSION_GROUPDIVID] == 1){ ?>
           
			<tr  style="display:none;"> <td align="right">Cabang:</td> <td align="left"><input type="text" name="m_cabang_id" id="m_cabang_id" value="<?php echo $_SESSION[SESSION_CABANGID]?>"></td> </tr><tr > 
           <?php }else{ ?>
			<tr > <td align="right">Cabang:</td> <td align="left"><?php echo form_dropdown("m_cabang_id",$m_cabang,"","id=m_cabang_id"); ?></td> </tr><tr > 
           
           <?php } ?>
           
			<tr > <td align="right">Tanggal Proyek:</td> <td align="left"><input id="project_date" name="project_date" /></td> </tr>
			<tr > <td align="right">Nilai Proyek:</td> <td align="left"><input id="project_value" name="project_value" class="currency" /></td> </tr>
           
			
			<tr>
				<td align="right">
                    <img hidden=hidden class="loading" src="<?php echo base_url("../") ?>assets/images/loader.gif" width=36 height=36>
                </td>
				<td style="padding-top: 10px;" align="right">
					<input style="margin-right: 5px;" type="button" id="btn-save-project" value="Simpan"  />
					<input id="btn-cancel-project" type="reset" value="Batal" /></td>
			</tr>
		</table>
	</form>